﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace OfficeServer
{
    internal class Util
    {
        private static readonly StringBuilder Sb = new StringBuilder();

        public static void Log(string s)
        {
            lock (Sb)
            {
                var sw = new StreamWriter(@"D:\[Thesis]\FillService.txt", true);
                sw.WriteLine(s);
                sw.Close();
            }
        }

        public static string FromBytesToString(byte[] bytes)
        {
            string s = "";
            if (bytes.Length > 0)
            {
                bytes[0].ToString();
                for (int i = 0; i < bytes.Length; i++)
                {
                    s += "." + i;
                }
            }
            return s;
        }

        public static byte[] FromStringToBytes(string s)
        {
            string[] temp = s.Split(new[] { '.' });
            var bytes = new byte[temp.Length];
            for (int i = 0; i < temp.Length; i++)
                bytes[i] = byte.Parse(temp[i]);
            return bytes;
        }

        private static Random random = new Random((int)DateTime.Now.Ticks);
        public static string RandomString(int size)
        {
            StringBuilder builder = new StringBuilder();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }
            return builder.ToString();
        }
    }
}
